package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.NursingProjectDto;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static com.zzyl.common.utils.PageUtils.startPage;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public static final String NURSING_PROJECT_ALL = "nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        int result = nursingProjectMapper.insert(nursingProject);
        deleteCache();
        return result;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        int result = nursingProjectMapper.updateById(nursingProject);
        deleteCache();
        return result;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int result = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        deleteCache();
        return result;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> nursingProjectVoList = (List<NursingProjectVo>) redisTemplate.opsForValue()
                                                                                            .get(NURSING_PROJECT_ALL);
        if (CollectionUtil.isNotEmpty(nursingProjectVoList)) {
            return nursingProjectVoList;
        }
        nursingProjectVoList = nursingProjectMapper.getAllProjects();
        redisTemplate.opsForValue().set(NURSING_PROJECT_ALL, nursingProjectVoList, 12, TimeUnit.HOURS);
        return nursingProjectVoList;
    }

    private void deleteCache() {
        redisTemplate.delete(NURSING_PROJECT_ALL);
    }

    /**
     * 分页查询护理项目列表
     *
     * @return 护理项目
     */
    @Override
    public TableDataInfo<NursingProject> selectPage(NursingProjectDto nursingProjectDto) {

        TableDataInfo<NursingProject> tableDataInfo = new TableDataInfo<>();

        Page<NursingProject> page = new Page<>(nursingProjectDto.getPageNum(), nursingProjectDto.getPageSize());
        lambdaQuery()
                .like(
                        StringUtils.isNotEmpty(nursingProjectDto.getName()), NursingProject::getName,
                        nursingProjectDto.getName()
                     )
                .eq(nursingProjectDto.getStatus() != null, NursingProject::getStatus, nursingProjectDto.getStatus())
                .page(page);

        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }
}
